#
#   Enemy.py
#
#   This program is free software; you can redistribute it and/or modify
#   it under the terms of the GNU General Public License as published by
#   the Free Software Foundation; version 2 of the License.
#
#   This program is distributed in the hope that it will be useful,
#   but WITHOUT ANY WARRANTY; without even the implied warranty of
#   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#   GNU General Public License for more details.
#
#   Copyright
#   Author: Nicholas F. Hoover
#   Contributors: Salvatore S. Gionfriddo
#   Created: 2007.06.25
#   Last Modified: 2007.07.24
#
from Character import Character


class Enemy(Character):

##############
# ANIMATIONS #
##############

    NUM_ANIMATIONS = 2
    (RUN_SIDE,
     BITE) = range(NUM_ANIMATIONS)  
    
    LEFT = NUM_ANIMATIONS
    
    ###############
    # FRAME TICKS #
    ###############
    RUN_ANIMATION_FRAME_TICKS = [1,2]
    BITE_ANIMATION_FRAME_TICKS = 7
    
    ##############
    # KEY FRAMES #
    ##############
    BITE_KEY_FRAME = 1
      
    DEFAULT_ANIMATION = RUN_SIDE
    
########
# MISC #
########
    TARGET_REACHED_X_RANGE = 5
    TARGET_REACHED_Y_RANGE = 4

    DEFAULT_POSITION = (0,0)
    RECT_SIZE = (30,50)

    COLLISION_RECT_SIZE = (10,2)
    COLLISION_RECT_POSITION = (10,48)

    #Name: moveTowardTarget
    #Description: Directs an Enemy toward the closest side of the targetRect and the bottom
    def moveTowardTarget(self, targetRect):
        """
        Moves the Enemy toward to closest side and bottom of the targetRect
        within the TARGET_REACHED ranges.
        Returns true if the target has been reached.
        """
        inXRange = False
        inYRange = False
        if self.rect.right < targetRect.centerx - Enemy.TARGET_REACHED_X_RANGE:
            self.currentAnimation = self.RUN_SIDE
            self.facing = self.RIGHT
        elif self.rect.left > targetRect.centerx + Enemy.TARGET_REACHED_X_RANGE:
            self.currentAnimation = self.RUN_SIDE + self.LEFT
            self.facing = self.LEFT
        else:
            inXRange = True


        if self.rect.bottom < targetRect.bottom - Enemy.TARGET_REACHED_Y_RANGE:
            #self.rect.top += self.Y_MOVEMENT
            y_movement = self.Y_MOVEMENT
        elif self.rect.bottom > targetRect.bottom + Enemy.TARGET_REACHED_Y_RANGE:
            #self.rect.top -= self.Y_MOVEMENT
            y_movement = -self.Y_MOVEMENT
        else:
            inYRange = True
            y_movement = 0

        return (inXRange and inYRange, y_movement)
            

    def update(self, evilGregRect, collision = None):
        if self.updateCurrentAnimation():
            (hitEvilGreg, y) = self.moveTowardTarget(evilGregRect)
        
            if hitEvilGreg:
                self.setAnimation(self.BITE, reset = False)
            else:
                self.setAnimation(self.RUN_SIDE, reset = False)

            self.updateCurrentImage()
            
            #HACK
            movement = self.MOVEMENTS[self.getAnimationType()]
            if self.currentAnimation == self.RUN_SIDE + self.LEFT:
                movement = (-movement[0], movement[1])
            
            self.move((movement[0],y), collision, True)

